import { request } from 'umi';
let data = {
  namespace: 'login',
  state: {
    access_token: '',
    timeId: '',
  },
  effects: {
    *getToken({ payload }, { call, put }) {
      const res = yield call(getData);
      put({
        type: 'setToken',
        payload: res.access_token,
      });
    },
  },
  reducers: {
    setState(state, { payload: { access_token, timeId } }) {
      return {
        ...state,
        access_token,
        timeId,
      };
    },
    setToken(state, { payload: { access_token } }) {
      return {
        ...state,
        ...access_token,
      };
    },
  },
};
const getData = async () => {
  return request('/api/auth/refresh', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${data.state.access_token}`,
    },
  });
};
export default data;
